package com.six.backend.ws;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.six.backend.dao.InformDao;
import com.six.backend.dao.ToolDao;
import com.six.backend.pojo.InformMessage;
import com.six.backend.pojo.Questionnaire;
import com.six.backend.service.ConferenceService;
import com.six.backend.utils.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: skyler
 * @Date: 2021/12/18 9:04
 */
@ServerEndpoint(value = "/inform",configurator = GetHttpSessionConfigurator.class)
@Component
public class InformEndpoint {

    private static Map<String,InformEndpoint> onlineUsers = new ConcurrentHashMap<>();

    private Session session;

    private HttpSession httpSession;

    private static ToolDao toolDao;

    private static InformDao informDao;

    @Autowired
    public  void setApplicationContext(ToolDao toolDao, InformDao informDao) {
        InformEndpoint.informDao = informDao;
        InformEndpoint.toolDao = toolDao;
    }

    @OnOpen
    public void onOpen(Session session, EndpointConfig config) {
        this.session = session;

        HttpSession httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());

        this.httpSession = httpSession;

        String username = (String) httpSession.getAttribute("user");

        onlineUsers.put(username,this);
    }

    private Set<String> getNames(Map<String,InformEndpoint> onlineUsers) {
        return onlineUsers.keySet();
    }

    private void broadcastUsers(String message, List<String> users) {
        for (String user : users) {
            try {
                InformEndpoint informEndpoint = onlineUsers.get(user);
                if (informEndpoint != null) {
                    informEndpoint.session.getBasicRemote().sendText(message);
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    @OnMessage
    public void onMessage(String message,Session session) {
        try {
            ObjectMapper mapper = new ObjectMapper();

            InformMessage informMessage = mapper.readValue(message, InformMessage.class);

            List<String> toName = informMessage.getToName();

            Integer delphiId = informMessage.getDelphiId();

            Questionnaire questionnaire = toolDao.selectQuestionnaireByDelphiId(delphiId);
            Integer questionnaireId = questionnaire.getId();

            boolean inform = informMessage.isInform();

            String username = (String) httpSession.getAttribute("user");

            String resultMessage = MessageUtils.getMessage(inform,false,false,username,null,questionnaireId,null);

            for (String s : toName) {
                informDao.insertInform(username,s,questionnaireId,new Date());
                if (onlineUsers.get(s) != null) {
                    onlineUsers.get(s).session.getBasicRemote().sendText(resultMessage);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

    }

    @OnClose
    public void onClose(Session session) {

        String username = (String) httpSession.getAttribute("user");

        onlineUsers.remove(username);

    }

    @OnError
    public void onError(Throwable throwable,Session session) {
        String username = (String) httpSession.getAttribute("user");

        onlineUsers.remove(username);
    }




}
